package lowijs.util.logging;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

import android.util.Log;

public class OneLineFormatter extends Formatter {
	private static final DateFormat FORMAT = new SimpleDateFormat("HH:mm:ss");

	@Override
	public String format(LogRecord record) {
		String text = null;

		try {
			if (record.getThrown() == null) {
				text = FORMAT.format(new Date(record.getMillis())) + " " + record.getLevel().getName() + ": " + record.getMessage() + "\n";
			} else {
				StringWriter string = new StringWriter();
				PrintWriter writer = new PrintWriter(string);
				writer.println(FORMAT.format(new Date(record.getMillis())) + " " + record.getLevel().getName() + ": " + record.getMessage());
				record.getThrown().printStackTrace(writer);
				writer.flush();
				writer.close();
				text = string.toString();
			}
		} catch (Exception e) {
			Log.e("OneLineFormatter", e.getMessage(), e);
			text = "Error!";
		}

		return text;

	}
}
